import request from '@/utils/request'
import store from '@/store' // todo:chace 
import { getToken } from '@/utils/auth'

// 菜单 
export function getMenu() {
    return request({
        url: '/getMenu',
        method: 'get',
    }).then(res => {
        store.commit('app/SET_RAW_MENU', res.data)
        return res;
    })
}

// 导出
export function exportData({ data: { fileName, exportType, headType, fields, module: templateCode }, listFormData }) {
    //参数转换
    const coverParamsToQuery = function (params) {
        // "a=c&b=a&c=c"
        let paramsArray = []
        for (let key in params) {
            paramsArray.push(`${key}=${params[key]}`)
        }
        return paramsArray.join("&")
    }
    //带参数下载
    const getRequestUrl = (url, params) => {
        // openLoading();
        let accessToken = getToken();
        params['ytoken'] = accessToken
        return url + "?" + coverParamsToQuery(params);
    };
    
    let newCondition = {}
    newCondition = listFormData
    let columns = []
    let symbols = []
    let words = []
    let keyWord = ""
    let keyParam = ""
    let noExport = ['keyWord', 'keyParam', 'pageNum', 'pageSize', 'totalNum', 'keyword', 'total', 'keyName', '_change_key']

    for (let i in newCondition) {
        if (i.indexOf("_") !== 0 &&
            !noExport.filter(val => {
                return val == newCondition[i]
            })[0]) {
            columns.push(i)
            symbols.push("eq")
            words.push(newCondition[i])
        }
    }
    if (newCondition.keyWord || newCondition.keyword) {
        keyWord = newCondition.keyWord ? newCondition.keyWord : newCondition.keyword
        keyParam = newCondition.keyParam ? newCondition.keyParam : newCondition.keyName
    }

    let keyList = fields.join(",")
    columns = columns.join(",")
    symbols = symbols.join(",")
    words = words.join(",")
    let query = []
    const keys = Object.keys(query)
    for (let i = 0; i < keys.length; i++) {
        var key = keys[i]
        if (key != "pageSize" && key != "pageNum" && key != "totalNum") {
            query.push(key + ":" + query[key])
        }
    }

    const params = {
        keyList: keyList,
        templateCode,
        columns,
        symbols,
        words,
        query,
        keyParam,
        keyWord,
    }
    return getRequestUrl(`../yethan/ImportExport/download/${exportType}/${fileName}/${headType}`, params)
}

// 获取需要导出的字段
export function exportItem(templateCode){
    return request({
        url: `/data/template/listAll`,
        method: 'get',
        params: {
            templateCode
        }
    }).then(res=>res.data)
}

// 导入 - 获取文件限制
export function getLimit(module){
    return request({
        url: `/public/sys/config/upload/${module}`,
        method: 'get',
    })
}

// 导入 - 自定义导入字段
export function importFileCustom(files, params){
    const data = new FormData() 

    data.append('files', files);
    for (let k in params) {
        data.append(k, params[k]);
    }

    return request({
        url: '/ImportExport/uploadData',
        method: 'post',
        data
    })
}

// 导入 - 确认
export function importFileSure(fileId, module, check, params){ 
    return request({
        url: `/ImportExport/upload/${fileId}/${module}/${check}`,
        method: 'post',
        data:params
    })
}
// 导入 - 撤销导入
export function deleteUpload(rollBackKey){
    return request({
        url: `/ImportExport/uploadRollBack`,
        method: 'delete',
        params:{
            rollBackKey
        }
    })
}

// 字典接口
export function getDict(dictCode) {
    return request({
        url: `/public/dict/${dictCode}`,
        method: 'get',
    }).then(res => {
        const dictList = res.data[dictCode]
        const dictInfo = res.data[dictCode + "_info"]
        return {
            dictList: dictList,
            dictInfo: dictInfo,
        }
    })
}

// 网站信息
export function getWebInfo() {
    return request({
        url: `/public/sys/config/web`,
        method: 'get',
    }).then(res => {
        return res.data
    })
} 